package com.xique.park.service.biz.service.impl;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.google.common.collect.HashMultimap;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.bean.domain.ParkCarMngtype;
import com.xique.park.bean.domain.ParkChargeCarFixed;
import com.xique.park.bean.vo.app.ParkCarMngtypeVo;
import com.xique.park.bean.vo.comm.MngTypeVo;
import com.xique.park.service.biz.service.IParkCarMngtypeBizService;
import com.xique.park.service.service.IParkCarMngtypeService;
import com.xique.park.service.service.IParkChargeCarFixedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 车辆管理类型 service
 *
 * @author: xique
 */
@Service
public class ParkCarMngtypeBizServiceImpl implements IParkCarMngtypeBizService {

    @Autowired
    private IParkCarMngtypeService parkCarMngtypeService;

    @Autowired
    private IParkChargeCarFixedService parkChargeCarFixedService;


    @Override
    public AjaxResult getParkCarMngtypeList(ParkCarMngtype parkCarMngtype) {
        List<ParkCarMngtype> list = parkCarMngtypeService.selectParkCarMngtypeList(parkCarMngtype);
        HashMultimap<String,ParkCarMngtypeVo> multimap = HashMultimap.create();
        list.stream().map(result -> {
            ParkCarMngtypeVo vo = new ParkCarMngtypeVo();
            vo.setMngtypeId(result.getId());
            vo.setMngtypeName(result.getName());
            vo.setMngtype(result.getMngType());
            multimap.put(vo.getMngtype(),vo);
            return vo;
        }).collect(Collectors.toList());

        Set<String>  typeSet = list.stream().map(ParkCarMngtype::getMngType).collect(Collectors.toSet());
        ArrayList<ParkCarMngtypeVo> parkCarMngtypeVos = new ArrayList<>();

        for (String s : typeSet) {
            if ("1".equals(s)||"2".equals(s)||"9".equals(s)||"10".equals(s)){
                ParkCarMngtypeVo parkCarMngtypeVo = new ParkCarMngtypeVo();
                parkCarMngtypeVo.setMngtype(s);
                parkCarMngtypeVo.setParkCarMngtypeVos(multimap.get(s));
                parkCarMngtypeVos.add(parkCarMngtypeVo);
            };
        }

        return AjaxResult.success(parkCarMngtypeVos);
    }

    @Override
    public AjaxResult getParkCarMngtypeList() {
        ParkCarMngtype parkCarMngtype = new ParkCarMngtype();
        parkCarMngtype.setParkId(SecurityUtils.getParkId());
        List<ParkCarMngtype> list = parkCarMngtypeService.selectParkCarMngtypeList(parkCarMngtype);
        Map<String, Object> map = new HashMap<>(1);
        List<MngTypeVo> vos = list.stream().map(result -> {
            MngTypeVo vo = new MngTypeVo();
            vo.setMngTypeId(result.getId());
            vo.setMngTypeName(result.getName());
            vo.setMngType(result.getMngType());
            return vo;
        }).collect(Collectors.toList());
        map.put("list", vos);
        return AjaxResult.success(map);
    }

    @Override
    public ParkChargeCarFixed getParkChargeFixed(Long mngtypeId) {
        ParkCarMngtype parkCarMngtype = parkCarMngtypeService.selectParkCarMngtypeById(mngtypeId);
        if (null != parkCarMngtype && null != parkCarMngtype.getChargeId() && 0 != parkCarMngtype.getChargeId()) {
            return parkChargeCarFixedService.selectParkChargeCarFixedById(parkCarMngtype.getChargeId());
        } else {
            return null;
        }
    }
}
